import java.nio.channels.MulticastChannel;
import java.util.Arrays;

public class testDemo {
    //递归求一个数每一位和
        public static void main1(String[] args) {
            int num = 1729;
            System.out.println(sum(num));
        }
        public static int sum (int a){
            if(a < 9){
                return a;
            }
            return a % 10 + sum(a / 10);
        }
    public static void main2(String[] args) {
        int num = 1729;
        print(num);
    }
    public static void print (int a){
        if(a > 9){
            print(a/10);
        }
        System.out.println(a%10+"");
    }

    public static int sumInt(int n) {
        if (n == 1) {
            return 1;
        }
        return n+sumInt(n-1);
    }
    public static void main3(String[] args) {
        int n=10;
        System.out.println(sumInt(n));
    }
    //递归求n的阶乘
    public static int mul(int n) {
        if (n == 1||n==2) {
            return n;
        }
        return n* mul(n-1);
    }
    public static void main4(String[] args) {
        int n=5;
        System.out.println(mul(n));
    }
        //数组所有元素的平均值
        public static void main5(String[] args){
            int[] arr={1,2,3,4,5,6,7};
            System.out.println(avg(arr));
        }
        public static double avg(int[] a){
            int ret=0;
            double sum;
            for(int x:a){
                ret+=x;
            }
            sum=ret/a.length;
            return sum;
        }
        //数组所有参数之和
        public static void main6(String[] args){
            int[] arr={1,2,3,4,5,6,7};
            System.out.println(sum(arr));
        }
        public static double sum(int[] a){
        int ret=0;
        double sum;
        for(int x:a){
            ret+=x;
        }

        return ret;
    }
    //循环将数组中的每个元素乘以2
    public static void main7(String[] args) {
        int[]arr={1,2,3};
        transform(arr);
    }
    public static void transform(int[] arr2) {
        for (int x:arr2){
            System.out.println(x*2);
        }
    }
    //实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.
    public static void main8(String[] args) {
            int[] arr={1,2,3};
            printArray(arr);
    }
    public static void printArray(int[] arr) {
        for (int x:arr){
            System.out.println(x);
        }
    }

    public static void main9(String[] args) {
        int[] arr=new int[100];
        for (int i = 0; i <100 ; i++) {
            arr[i]=i+1;
        }
        System.out.println(Arrays.toString(arr));
    }

    public static void main10(String[] args) {
        int[] arr={3,6,5,9,1};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]) {
                    int tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                }
            }
        }
    }

    public static boolean induge(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i] > arr[i+1]) {
                return false;
            }
        }
        return true;
    }
    public static void main11(String[] args) {
        int[] arr={2,6,7,9};
        System.out.println(induge(arr));
    }
    //二分查找
    public static int binarySearch(int[]array,int key) {
        int left=0;
        int right= array.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(array[mid]>key){
                right=mid-1;
            }else if (array[mid]<left){
                left=mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static void main12(String[] args) {
        int[] array={1,2,3,4,5,15,19};
        System.out.println(binarySearch(array,2));
    }
//复制数组
    public static int[] copyArr(int[] arr) {
        int[] arr2=new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            arr2[i]=arr[i];
        }
        return arr2;
    }
    public static void main13(String[] args) {
        int[] arr={2,6,3,8};
        int[] newarr=copyArr(arr);
        System.out.println(Arrays.toString(newarr));
    }
    //实现一个方法 toString, 把一个整型数组转换成字符串. 例如数组 {1, 2, 3} , 返回的字符串为 "[1, 2, 3]", 注意逗号的位置和数量.
    public static void main14(String[] args) {
        int[] arr={2,6,9,40};
        String a=toString(arr);
        System.out.println(a);
    }
    public static String toString(int[] arr) {
        String a="[";
        for (int i = 0; i < arr.length; i++) {
            a+=arr[i];
            if (i!= arr.length-1){
                a+=",";
            }
        }
        a+="]";
        return a;
    }
    //给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。
    public static void main(String[] args) {
        int[] arr={2,6,8,6,2};
        System.out.println(singleNum(arr));
    }

    public static int singleNum(int[] arr) {
        int single=0;
        for (int nums:arr){
            single=single^nums;
        }
        return single;
    }
}




